#### TABLE F.1: HETEROGENEOUS EFFECTS BY TOLERANCE
#### Treatment effect interactions with baseline tolerance

rm(list = ls())
source("./2_code/00_setup.R")

#### STUDY 1 ####

data <- fread(paste0(data_path, "data_study1.csv"), header = TRUE)
data$worked <- as.factor(data$worked)

# Exclusion outcomes
entry_eco <- lm_robust(exclusion_s ~ treat_economic * very_tolerant, 
                       data = data[(treat=='economic' | treat=='control')])
entry_hum <- lm_robust(exclusion_s ~ treat_humanitarian * very_tolerant, 
                       data = data[(treat=='humanitarian' | treat=='control')])

# Prejudice outcomes
prejudice_eco <- lm_robust(prejudice_s ~ treat_economic * very_tolerant, 
                           data = data[(treat=='economic' | treat=='control')])
prejudice_hum <- lm_robust(prejudice_s ~ treat_humanitarian * very_tolerant, 
                           data = data[(treat=='humanitarian' | treat=='control')])


#### STUDY 2 ####

data2 <- fread(paste0(data_path, "data_study2.csv"), header = TRUE)
data2$worked <- as.factor(data2$worked)

# Exclusion outcomes
entry_eco2 <- lm_robust(exclusion_s ~ treat_economic * very_tolerant, 
                        data = data2[(treat=='economic' | treat=='control')])
entry_hum2 <- lm_robust(exclusion_s ~ treat_humanitarian * very_tolerant, 
                        data = data2[(treat=='humanitarian' | treat=='control')])

# Prejudice outcomes
prejudice_eco2 <- lm_robust(prejudice_s ~ treat_economic * very_tolerant, 
                            data = data2[(treat=='economic' | treat=='control')])
prejudice_hum2 <- lm_robust(prejudice_s ~ treat_humanitarian * very_tolerant, 
                            data = data2[(treat=='humanitarian' | treat=='control')])


#### HELPER FUNCTIONS ####

extract_coefs <- function(model) {
  coefs <- coef(model)
  ses <- model$std.error
  pvals <- model$p.value
  
  stars <- ifelse(pvals < 0.001, "***",
                  ifelse(pvals < 0.01, "**",
                         ifelse(pvals < 0.05, "*", "")))
  
  data.frame(
    Estimate = coefs,
    SE = ses,
    Stars = stars,
    stringsAsFactors = FALSE
  )
}

format_coef <- function(coef_data, row_name) {
  if (row_name %in% rownames(coef_data)) {
    paste0(sprintf("%.3f", coef_data[row_name, "Estimate"]), coef_data[row_name, "Stars"])
  } else {
    ""
  }
}

format_se <- function(coef_data, row_name) {
  if (row_name %in% rownames(coef_data)) {
    paste0("(", sprintf("%.3f", coef_data[row_name, "SE"]), ")")
  } else {
    ""
  }
}


#### EXTRACT COEFFICIENTS ####

study1_eco_exclusion <- extract_coefs(entry_eco)
study1_hum_exclusion <- extract_coefs(entry_hum)
study1_eco_prejudice <- extract_coefs(prejudice_eco)
study1_hum_prejudice <- extract_coefs(prejudice_hum)

study2_eco_exclusion <- extract_coefs(entry_eco2)
study2_hum_exclusion <- extract_coefs(entry_hum2)
study2_eco_prejudice <- extract_coefs(prejudice_eco2)
study2_hum_prejudice <- extract_coefs(prejudice_hum2)


#### BUILD LATEX TABLE ####

latex_table <- paste0(
  "\\begin{table}[!htbp]\\centering
\\caption{Heterogeneous Treatment Effects by Tolerance Level}
\\label{tab:heterogeneous_effects}
\\begin{minipage}{\\textwidth}
\\subsection*{Panel A: Study 1}
\\begin{tabular}{lcccc}
\\hline\\hline
 & \\multicolumn{2}{c}{Exclusion} & \\multicolumn{2}{c}{Prejudice} \\\\
\\cline{2-3} \\cline{4-5}
 & Economic & Humanitarian & Economic & Humanitarian \\\\
\\hline
Treatment & ", format_coef(study1_eco_exclusion, "treat_economic"), " & ", format_coef(study1_hum_exclusion, "treat_humanitarian"), " & ", format_coef(study1_eco_prejudice, "treat_economic"), " & ", format_coef(study1_hum_prejudice, "treat_humanitarian"), " \\\\
 & ", format_se(study1_eco_exclusion, "treat_economic"), " & ", format_se(study1_hum_exclusion, "treat_humanitarian"), " & ", format_se(study1_eco_prejudice, "treat_economic"), " & ", format_se(study1_hum_prejudice, "treat_humanitarian"), " \\\\
Very Tolerant & ", format_coef(study1_eco_exclusion, "very_tolerant"), " & ", format_coef(study1_hum_exclusion, "very_tolerant"), " & ", format_coef(study1_eco_prejudice, "very_tolerant"), " & ", format_coef(study1_hum_prejudice, "very_tolerant"), " \\\\
 & ", format_se(study1_eco_exclusion, "very_tolerant"), " & ", format_se(study1_hum_exclusion, "very_tolerant"), " & ", format_se(study1_eco_prejudice, "very_tolerant"), " & ", format_se(study1_hum_prejudice, "very_tolerant"), " \\\\
Treatment $\\times$ Very Tolerant & ", format_coef(study1_eco_exclusion, "treat_economic:very_tolerant"), " & ", format_coef(study1_hum_exclusion, "treat_humanitarian:very_tolerant"), " & ", format_coef(study1_eco_prejudice, "treat_economic:very_tolerant"), " & ", format_coef(study1_hum_prejudice, "treat_humanitarian:very_tolerant"), " \\\\
 & ", format_se(study1_eco_exclusion, "treat_economic:very_tolerant"), " & ", format_se(study1_hum_exclusion, "treat_humanitarian:very_tolerant"), " & ", format_se(study1_eco_prejudice, "treat_economic:very_tolerant"), " & ", format_se(study1_hum_prejudice, "treat_humanitarian:very_tolerant"), " \\\\
Constant & ", format_coef(study1_eco_exclusion, "(Intercept)"), " & ", format_coef(study1_hum_exclusion, "(Intercept)"), " & ", format_coef(study1_eco_prejudice, "(Intercept)"), " & ", format_coef(study1_hum_prejudice, "(Intercept)"), " \\\\
 & ", format_se(study1_eco_exclusion, "(Intercept)"), " & ", format_se(study1_hum_exclusion, "(Intercept)"), " & ", format_se(study1_eco_prejudice, "(Intercept)"), " & ", format_se(study1_hum_prejudice, "(Intercept)"), " \\\\
\\hline
Observations & ", entry_eco$nobs, " & ", entry_hum$nobs, " & ", prejudice_eco$nobs, " & ", prejudice_hum$nobs, " \\\\
R$^2$ & ", sprintf("%.3f", entry_eco$r.squared), " & ", sprintf("%.3f", entry_hum$r.squared), " & ", sprintf("%.3f", prejudice_eco$r.squared), " & ", sprintf("%.3f", prejudice_hum$r.squared), " \\\\
\\hline
\\end{tabular}
\\end{minipage}

\\vspace{0.5cm}

\\begin{minipage}{\\textwidth}
\\subsection*{Panel B: Study 2}
\\begin{tabular}{lcccc}
\\hline\\hline
 & \\multicolumn{2}{c}{Exclusion} & \\multicolumn{2}{c}{Prejudice} \\\\
\\cline{2-3} \\cline{4-5}
 & Economic & Humanitarian & Economic & Humanitarian \\\\
\\hline
Treatment & ", format_coef(study2_eco_exclusion, "treat_economic"), " & ", format_coef(study2_hum_exclusion, "treat_humanitarian"), " & ", format_coef(study2_eco_prejudice, "treat_economic"), " & ", format_coef(study2_hum_prejudice, "treat_humanitarian"), " \\\\
 & ", format_se(study2_eco_exclusion, "treat_economic"), " & ", format_se(study2_hum_exclusion, "treat_humanitarian"), " & ", format_se(study2_eco_prejudice, "treat_economic"), " & ", format_se(study2_hum_prejudice, "treat_humanitarian"), " \\\\
Very Tolerant & ", format_coef(study2_eco_exclusion, "very_tolerant"), " & ", format_coef(study2_hum_exclusion, "very_tolerant"), " & ", format_coef(study2_eco_prejudice, "very_tolerant"), " & ", format_coef(study2_hum_prejudice, "very_tolerant"), " \\\\
 & ", format_se(study2_eco_exclusion, "very_tolerant"), " & ", format_se(study2_hum_exclusion, "very_tolerant"), " & ", format_se(study2_eco_prejudice, "very_tolerant"), " & ", format_se(study2_hum_prejudice, "very_tolerant"), " \\\\
Treatment $\\times$ Very Tolerant & ", format_coef(study2_eco_exclusion, "treat_economic:very_tolerant"), " & ", format_coef(study2_hum_exclusion, "treat_humanitarian:very_tolerant"), " & ", format_coef(study2_eco_prejudice, "treat_economic:very_tolerant"), " & ", format_coef(study2_hum_prejudice, "treat_humanitarian:very_tolerant"), " \\\\
 & ", format_se(study2_eco_exclusion, "treat_economic:very_tolerant"), " & ", format_se(study2_hum_exclusion, "treat_humanitarian:very_tolerant"), " & ", format_se(study2_eco_prejudice, "treat_economic:very_tolerant"), " & ", format_se(study2_hum_prejudice, "treat_humanitarian:very_tolerant"), " \\\\
Constant & ", format_coef(study2_eco_exclusion, "(Intercept)"), " & ", format_coef(study2_hum_exclusion, "(Intercept)"), " & ", format_coef(study2_eco_prejudice, "(Intercept)"), " & ", format_coef(study2_hum_prejudice, "(Intercept)"), " \\\\
 & ", format_se(study2_eco_exclusion, "(Intercept)"), " & ", format_se(study2_hum_exclusion, "(Intercept)"), " & ", format_se(study2_eco_prejudice, "(Intercept)"), " & ", format_se(study2_hum_prejudice, "(Intercept)"), " \\\\
\\hline
Observations & ", entry_eco2$nobs, " & ", entry_hum2$nobs, " & ", prejudice_eco2$nobs, " & ", prejudice_hum2$nobs, " \\\\
R$^2$ & ", sprintf("%.3f", entry_eco2$r.squared), " & ", sprintf("%.3f", entry_hum2$r.squared), " & ", sprintf("%.3f", prejudice_eco2$r.squared), " & ", sprintf("%.3f", prejudice_hum2$r.squared), " \\\\
\\hline
\\end{tabular}
\\end{minipage}

\\begin{tablenotes}
\\small
\\item \\textit{Note:} $^{*}p<0.05$; $^{**}p<0.01$; $^{***}p<0.001$.
\\item 'Very Tolerant' is a dummy variable indicating respondents who scored 6 or 7 on the immigrant warmth scale.
\\end{tablenotes}
\\end{table}"
)

writeLines(latex_table, paste0(tables_path, "table_F1.tex"))


